const request = require('../../utils/request')

Page({
  data: {
    doctorReplies: [],
    loading: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getDoctorReplies()
  },

  /**
   * 获取医生回复列表
   */
  getDoctorReplies() {
    this.setData({ loading: true })
    
    request.get('/student/doctor/getDoctorConsulation', {}, true)
      .then(res => {
        this.setData({ loading: false })
        
        if (res.code === 0 && res.data) {
          console.log('获取到医生回复数据:', res.data)
          this.setData({
            doctorReplies: res.data
          })
        } else {
          wx.showToast({
            title: res.message || '获取医生回复失败',
            icon: 'none'
          })
          this.setData({
            doctorReplies: []
          })
        }
      })
      .catch(err => {
        console.error('获取医生回复失败', err)
        this.setData({ 
          loading: false,
          doctorReplies: []
        })
        wx.showToast({
          title: '获取医生回复失败',
          icon: 'none'
        })
      })
  },

  /**
   * 格式化日期时间
   */
  formatDateTime(dateTimeStr) {
    if (!dateTimeStr) return '';
    
    try {
      const dateObj = new Date(dateTimeStr);
      const year = dateObj.getFullYear();
      const month = String(dateObj.getMonth() + 1).padStart(2, '0');
      const day = String(dateObj.getDate()).padStart(2, '0');
      const hours = String(dateObj.getHours()).padStart(2, '0');
      const minutes = String(dateObj.getMinutes()).padStart(2, '0');
      
      return `${year}-${month}-${day} ${hours}:${minutes}`;
    } catch (e) {
      console.error('日期格式化错误', e);
      return dateTimeStr;
    }
  },

  /**
   * 下拉刷新
   */
  onPullDownRefresh() {
    this.getDoctorReplies()
    wx.stopPullDownRefresh()
  }
}) 